<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;

class System extends Controller
{
    public function webinfo()
    {
        $id = trim(input('param.tenantid'));
        $this->db = new SysDb();
        $one = $this->db->table('tenant_info')->where(['id' => $id])->item();
        
        exit(json_encode(['message' =>'succ','result'=> $one]));
    }
    
    public function webupdate()
    {
        $qq = trim(input('param.qq'));
            $keywords = trim(input('param.keywords'));
            $favicon = trim(input('param.types'));
            $totalBalance = trim(input('param.total_balance'));
            $wechat = trim(input('param.wechat'));
            $title = trim(input('param.title'));
            $descr = trim(input('param.descr'));
            $balance = trim(input('param.balance'));
            $rechargeAmount=trim(input('param.recharge_amount'));
            $phone = trim(input('param.phone'));
            $domainName = trim(input('param.domain_name'));
            $contactNumber = trim(input('param.contact_number'));
            $logo = trim(input('param.logo'));
            $company = trim(input('param.company'));
            $heads = trim(input('param.heads'));
            $id = trim(input('param.id'));
            $spreadAmount = trim(input('param.spread_amount'));
            $status = trim(input('param.status'));
            $array=array();
            array_push( $array,$favicon);
            array_push( $array, $wechat);
            array_push( $array, $logo);
            array_push( $array,$heads);
            foreach ($array as  $k=>&$value) {
                $needle = 'base64'; //判断是否包含a这个字符
                $tmparray = explode($needle, $value);
                if (count($tmparray) > 1) {
                    $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
                    if (strstr($value, ",")) {
                        $image = explode(',', $value);
                        $image = $image[1];
                    }
                    $path = "./uploads/" . date("Ymd", time());
                    $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
                    if (!is_dir($path)) { //判断目录是否存在 不存在就创建
                        mkdir($path, 0777, true);
                    }
                    $imageSrc = $path . "/" . $imageName; //图片名字
                    $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
                    $imageSrcs = $paths . "/" . $imageName; //图片名字
                    $imageSrcs = $paths . "/" . $imageName; //图片名字
                    if ($r) {
                        $value=$imageSrcs;
                    }
                }
            }
                $this->db = new SysDb();
                // $admin = $this->db->table('tenant_info')->where(['id ' => $id])->item();
                $admin['qq'] = $qq;
                $admin['keywords'] = $keywords;
                $admin['favicon'] = $array[0];
                $admin['total_balance'] = $totalBalance;
                $admin['wechat'] = $array[1];
                $admin['title'] = $title;
                $admin['descr'] = $descr;
                $admin['balance'] = $balance;
                $admin['recharge_amount'] = $rechargeAmount;
                $admin['phone'] = $phone;
                $admin['domain_name'] = $domainName;
                $admin['contact_number'] = $contactNumber;
                $admin['logo'] = $array[2];
                $admin['company'] = $company;
                $admin['heads'] = $array[3];
                // $admin['id'] = $id;
                $admin['spread_amount'] = $spreadAmount;
                $admin['status'] = $status;
                $admins = $this->db->table('tenant_info')->where(['id' => $id])->update($admin);
                if ($admins) {
                    exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
                }
    }
}

